#------------------------------------------------------------------------------
# Project:     Multi-Programmer Game Demo
# Name:        Ball.py
# Purpose:     Bouncing Ball Object
# 
# Authors:     Chris Spooner, Chris La-Roo
# Main Author: Chris Spooner
#
# Created:     11/30/2012
# Copyright:   (c) Chris Spooner 2012
# Licence:     New BSD
#------------------------------------------------------------------------------
import pygame, math, sys, random

class Ball():
    def __init__(self, speed, position, screenSize, waitMax = 50):
        self.surfaces = []
        self.surfaces += [pygame.image.load("rsc/ball/ballred.png")]
        self.surfaces += [pygame.image.load("rsc/ball/ballgreen.png")]
        self.frame = 0
        self.maxFrame = len(self.surfaces)-1
        self.surface = self.surfaces[self.frame]
        self.rect = self.surface.get_rect()
        self.speed = speed
        self.place(position)
        self.living = True
        if pygame.mixer:
            self.bounceSound = pygame.mixer.Sound("bounce.wav")
        self.screenWidth = screenSize[0]
        self.screenHeight = screenSize[1]
        self.radius = self.rect.width/2
        self.waitCount = 0
        self.waitMax = waitMax
        self.frame = 0
        self.maxFrame = len(self.surfaces) - 1
        
    def  __str__(self):
        return "I'm a ball " + str(self.rect.center) + str(self.speed) + str(self.living)
     
    def place(self, position):
        self.rect.center = position
        print "I've moved to", position
        
    def move(self):
        self.rect = self.rect.move(self.speed)
        print "I've moved", self.speed 
        if self.waitCount < self.waitMax:
            self.waitCount += 1
        else:
            self.waitCount = 0
            if self.frame < self.maxFrame:
                self.frame += 1
            else:
                self.frame = 0
            self.surface = self.surfaces[self.frame] 
        
    def collideFloor(self, screenHeight):
        print "trying to hit bottom of screen", screenHeight
    
    def collideWall(self, screenWidth, screenHeight):
        if (self.rect.left < 0 
            or self.rect.right > self.screenWidth):
            self.speed[0] = 0
        if (self.rect.top < 0 
            or self.rect.bottom >self.screenHeight):
            self.speed[1] = 0
        print "trying to hit edges of screen", screenWidth, screenHeight
        
    def collidePaddle(self, paddle):
        if (self.rect.right > paddle.rect.left 
            and self.rect.left < paddle.rect.right):
            if (self.rect.bottom > paddle.rect.top and 
                self.rect.top < paddle.rect.bottom):
                if (self.distToPoint(paddle.rect.center)
                    < self.radius + paddle.radius):  
                    self.speed[0] = self.speed[0] * -1
                    self.speed[1] = self.speed[1] * -1
                    paddle.speed[0] = paddle.speed[0] * -1
                    paddle.speed[1] = paddle.speed[1] * -1
        print "trying to hit paddle", str(paddle)
        #if pygame.mixer:
        #   self.bounceSound.play()
        
    def collideBall(self, other):
        print "trying to hit ", str(other)
        
    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))